'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  const { deviceId, updateData } = event;

  if (!deviceId || !updateData) {
    return {
      code: -1,
      message: '参数错误'
    };
  }

  try {
    // 更新设备信息
    const res = await db.collection('devices')
      .doc(deviceId)
      .update({
        ...updateData,
        update_time: Date.now()
      });

    if (res.updated === 1) {
      return {
        code: 0,
        message: '更新成功'
      };
    } else {
      return {
        code: -1,
        message: '设备不存在'
      };
    }
  } catch (e) {
    console.error('更新设备信息失败:', e);
    return {
      code: -1,
      message: '更新设备信息失败',
      error: e.message
    };
  }
}; 